package process

import (
	"communication-system/common/message"
	"communication-system/server/utils"
	"encoding/json"
	"fmt"
	"net"
	"os"
)

//show the figure of success
func ShowMenu() {
	fmt.Println("-------------------success login---------------")
	fmt.Println("---------------1. show the list of users-------")
	fmt.Println("---------------2. send message-----------------")
	fmt.Println("---------------3. info list--------------------")
	fmt.Println("---------------4. exit-------------------------")
	fmt.Println("Please choose(1-4):")
	var key int
	var content string

	smsProcess := &SmsProcess{}

	fmt.Scanf("%d\n", &key)
	switch key {
	case 1:
		//fmt.Println("show the list of users")
		outputOnlineUser()
	case 2:
		fmt.Println("Please input content that you want to say:")
		fmt.Scanf("%s\n", &content)
		smsProcess.SendGroupMes(content)
	case 3:
		fmt.Println("info lists")
	case 4:
		fmt.Println("exit...")
		os.Exit(0)
	default:
		fmt.Println("input err")
	}
}

//keep comunication with server
func serverProcessMes(conn net.Conn) {
	//create a transfer to read msg from server
	tf := &utils.Transfer{
		Conn: conn,
	}
	for {
		fmt.Println("client is reading msg from server")
		mes, err := tf.ReadPkg()
		if err != nil {
			fmt.Println("tf.ReadPkg err=", err)
			return
		}
		//fmt.Printf("mes=%v\n", mes)
		switch mes.Type {
		case message.NotifyUserStatusMesType:
			var notifyUserStatusMes message.NotifyUserStatusMes
			json.Unmarshal([]byte(mes.Data), &notifyUserStatusMes)
			updateUserStatus(&notifyUserStatusMes)
		case message.SmsMesType:
			outputGroupMes(&mes)
		default:
			fmt.Println("server return a unkown info")
		}
	}
}
